var loginToken = getItems("token");
var orgList = "";
var netList = "";
var firstOrgId = "";
if(loginToken || getUrlParam('token') != null){
	if(getUrlParam('token') != null){
		$(".headerUser").show();
		$(".headerUser .down").html(getUrlParam('userName'));
		loginToken = getUrlParam('token');
		setItems("token",getUrlParam('token'));
	}else {
		$(".headerUser").show();
		$(".headerUser .down").html(getItems("userName"))
	}
	//查询组织
	$.ajax({
		type:"GET",
		url:"/api/customer/baasorg/org/all",
		contentType: "application/json",
		headers:{
			Authorization:loginToken
		},
		success:function(data){
			if(data.status == 200){	
				if(data.data.rows.length!=0){
					$(".headerOrg").show();
					var OrgId=getItems("orgName");
					if(OrgId!=null){
						var str=getItems("orgName");
						$(".headerOrg .down").html( str);
					}else{
						setItems("orgId",data.data.rows[0].orgId);
						setItems("orgName",data.data.rows[0].orgName);
						$(".headerOrg .down").html(data.data.rows[0].orgName);
					}
					queryNetWorkName()
					// $(".headerOrg .down").html(data.data.rows[0].orgName);
					orgList = "";
					data.data.rows.map(function(currentValue){
						orgList += "<li orgId='"+currentValue.orgId+"' orgName='"+currentValue.orgName+"' onclick='getOrgName($(this))'>"+currentValue.orgName+"</li>";
					});
					$(".headerOrg .downList").html(orgList);
					orgNet()
				}else{
					$(".headerOrg .down").html('暂无网络');
					$(".headerNet .down").html('暂无网络');

					var netList = "<li a='a'>暂无网络</li>";
					$(".headerNet .downList").html(netList);
					$(".headerNet").show();

					$(".headerOrg .downList").html(netList);
					$(".headerOrg").show();
				}
			
			}
		}
	})
}else{
	$(".loginBtn").show();
}

function queryNetWorkName(){
	$.ajax({
		type:"GET",
		url:"/api/block/network/queryNetWorkName?orgId="+getItems("orgId"),
		contentType: "application/json",
		headers:{
			Authorization:loginToken,
		},
		success:function(data){
			// console.log('初始化');
			if(data.length!=0){
				if(data[0].nickname==undefined){
					localStorage.clear();
					$(".loginBtn").show();
					$(".headerUser,.headerOrg,.headerNet").hide();
				}else{
					$(".headerNet").show();
					
					var FabricFlag=getItems("nickname");
					if(FabricFlag!=null){
						var str=getItems("nickname")
						$(".headerNet .down").html( str);
					}else{
						setItems("nickname",data[0].nickname);
						setItems("networkName",data[0].networkName);
						$(".headerNet .down").html(data[0].nickname);
					}

					netList = "";
					data.map(function(currentValue){
						netList += "<li  networkname='"+currentValue.networkName+"' nickname='"+currentValue.nickname+"' onclick='getNetWorkName($(this))'>"+currentValue.nickname+"</li>";
					});
					$(".headerNet .downList").html(netList);
					hideDownList();
				}
			}else{
				// console.log('初始化没有网络======');
				$(".headerNet .down").html('暂无网络');
				netList = "<li a='a'>暂无网络</li>";
				$(".headerNet .downList").html(netList);
				$(".headerNet").show();
			}
		}
	})
}

//退出登录
$(".loginOut").click(function(){
	localStorage.clear();
	$(".loginBtn").show();
	$(".headerUser,.headerOrg,.headerNet").hide();
	getStr(window.location.href,'?')
})

function getStr(string,str){
  var str_before = string.split(str)[0];
	var str_after = string.split(str)[1];
	window.location.href = str_before
  // alert('前：'+str_before+' - 后：'+str_after);
}
//login
$(".loginBtn").click(function(){
	$(".overlay,.login").show();
});
$(".overlay").click(function(){
	$(".overlay,.login").hide();
});

$(".login button").click(function(){
	var loginData = {
		username : "",
		password : ""
	};
	$(this).prev(".loginError").html("")
	if($(this).prevAll(".loginName").val() == ""){
		$(this).prev(".loginError").html('<span class="waring"></span>请输入账号！');
		setTimeout(function(){
			$(".loginError").html("");
		},3000)
	}else if($(this).prevAll(".loginPsw").val() == ""){
		$(this).prev(".loginError").html('<span class="waring"></span>请输入密码！');
		setTimeout(function(){
			$(".loginError").html("");
		},3000)
	}else{
		loginData.username = $(this).prevAll(".loginName").val();
		loginData.password = $(this).prevAll(".loginPsw").val();
		$(".Rotation").show();
		$.ajax({
			type:"POST",
			url:"/api/auth/jwtcustomer/customertoken",
			contentType: "application/json",
			data: JSON.stringify(loginData),
			dataType: "json",
			success:function(data){
				$(".Rotation").hide();
				if(data.token == ""){
					$(".loginError").html('<span class="waring"></span>账户或密码错误！');
					setTimeout(function(){
						$(".loginError").html("");
					},3000)
					// console.log("账户或密码错误")
				}else{
					loginToken= data.token;
					setItems("userName",loginData.username);
					setItems("token",data.token);
					$(".login,.overlay,.loginBtn").hide();
					$(".headerUser").show();
					$(".headerUser .down").html(loginData.username)

					//查询组织
					$.ajax({
						type:"GET",
						url:"/api/customer/baasorg/org/all",
						contentType: "application/json",
						headers:{
							Authorization:loginToken
						},
						success:function(data){
							if(data.status == 200){
								if(data.data.rows.length!=0){
									firstOrgId = data.data.rows[0].orgId;
									setItems("orgId",data.data.rows[0].orgId);
									orgList = "";
									data.data.rows.map(function(currentValue){
										orgList += "<li orgId='"+currentValue.orgId+"'>"+currentValue.orgName+"</li>";
									});
									$(".headerOrg .downList").html(orgList);
									$(".headerOrg .down").html(data.data.rows[0].orgName);
									$(".headerOrg").show();
								}else{
									$(".headerOrg .down").html('暂无网络');
									$(".headerNet .down").html('暂无网络');
				
									var netList = "<li a='a'>暂无网络</li>";
									$(".headerNet .downList").html(netList);
									$(".headerNet").show();
				
									$(".headerOrg .downList").html(netList);
									$(".headerOrg").show();
								}

								//查询网络
								$.ajax({
									type:"GET",
									url:"/api/block/network/queryNetWorkName?orgId="+firstOrgId,
									contentType: "application/json",
									headers:{
										Authorization:loginToken,
									},
									success:function(data){
										if(data &&　data.length != 0){
											$(".headerNet .down").html(data[0].nickname);
											netList = "";
											data.map(function(currentValue){
												netList += "<li networkname='"+currentValue.networkName+"' nickname='"+currentValue.nickname+"' onclick='getNetWorkName($(this))'>"+currentValue.nickname+"</li>";
											});
											$(".headerNet .downList").html(netList);
											$(".headerNet").show();
											setItems("networkName",data[0].networkName);
											hideDownList();
											$.ajax({
												type:"POST",
												url:"/api/block/public/queryChannels",
												contentType: "application/json",
												data: JSON.stringify({RequestType:"QueryChannelsReq",FabricFlag:data[0].networkName,ChannelId:"mychannel"}),
												dataType: "json",
												headers:{
													Authorization:loginToken
												},
												success: function(data){  
													if(data && data.code == 200){
														var thisData = JSON.parse(data.data);
														setItems("Channel",thisData.ChannelList[0]);
													}else{
														setItems("Channel","mychannel");
													}
													window.location.href=window.location.href;	
												}
											})
										}else{
											$(".headerNet .down").html('暂无网络');
											var netList = "<li a='a'>暂无网络</li>";
											$(".headerNet .downList").html(netList);
											$(".headerNet").show();
											window.location.href=window.location.href;  
										}
									}
								})
							}
						}
					})
					
				}
			}
		})
	}
})
function getNetWorkName($this){
	setItems("networkName",$this.attr("networkname"))
	setItems("nickname",$this.attr("nickname"));
	window.location.href="";
};

function getOrgName($this){
	setItems("orgName",$this.attr("orgName"))
	setItems("orgId",$this.attr("orgId"));
	window.location.href="";
}

//组织
function orgNet(){
	$(".headerOrg .downList li").click(function(){
		// console.log($(this).attr("orgId"))
		//查询网络
		$.ajax({
			type:"GET",
			url:"/api/block/network/queryNetWorkName?orgId="+$(this).attr("orgId"),
			contentType: "application/json",
			headers:{
				Authorization:loginToken,
			},
			success:function(data){
				if(data){
					if(data.length!=0){
						$(".headerNet .down").html(data[0].nickname);
						netList = "";
						data.map(function(currentValue){
							netList += "<li a='a'>"+currentValue.nickname+"</li>";
						});
						$(".headerNet .downList").html(netList);
						$(".headerNet").show();
					}

					hideDownList()
				}
			}
		})
	})
	
}

function hideDownList(){
	$(".headerRt .downList li").click(function(){
		$(this).parents(".headerRt").find(".downBox").attr("off","true");
		$(this).parents(".headerRt").find(".downBox .downBg").removeClass("roateUp").addClass("roateDown");
		$(this).parents(".headerRt").find(".downBox").next(".downList").slideUp("fast");
		$(this).parents(".headerRt").find(".downBox").next(".up").fadeToggle("fast");
	})
}



//var off = true;
function downShow() {
	$(".downBox").click(function() {
		$(".downBox").not($(this)).each(function(){
			if($(this).attr("off") == "false"){
				$(this).attr("off","true");
				$(this).find(".downBg").removeClass("roateUp").addClass("roateDown");
				$(this).next(".downList").slideToggle("fast");
				$(this).next(".up").fadeToggle("fast");
			}
		})
		if($(this).attr("off") == "true") {
			$(this).find(".downBg").removeClass("roateDown").addClass("roateUp");
			$(this).attr("off","false");
		} else {
			$(this).find(".downBg").removeClass("roateUp").addClass("roateDown");
			$(this).attr("off","true");
		}
		$(this).next(".downList").slideToggle("fast");
		$(this).next(".up").fadeToggle("fast");

	})
}

downShow();	


function getItems(key){
	var getItemvalue = localStorage.getItem(key);
	return getItemvalue;
}

function setItems(key,keyValue){
	var localStoragevalue = localStorage.setItem(key,keyValue);
	return localStoragevalue;
}

function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); //匹配目标参数

	if(r != null)
		return unescape(r[2]);
	return null; //返回参数值
}